# Create your views here.
#from l2l.apps.blog.views import *
from l2ldjango.apps.jobsapp.models import *
from l2ldjango.apps.jobsapp.forms import *
from django.template import RequestContext 
from django.shortcuts import render_to_response, get_object_or_404
from l2ldjango.settings import DATABASES
from django.shortcuts import *
from django.contrib.auth.decorators import login_required
from l2ldjango.apps.usermanagement.views import *
from django.core.mail import EmailMessage



@login_required
def createJob(request):
    if request.method=='POST':
        form = JobForm(request.POST)
        if form.is_valid():
            job_obj=form.save()
            job_obj.posted_by=request.user
            job_obj.save()
            title="Successfully saved"
            message="Job posted."
            variables = RequestContext(request,{'title':title,'message':message}) 
            return render_to_response('pages/success_page.html',variables)
        else:
            err=form.errors
            variables = RequestContext(request,{'form':form,'message':err,'action':'/l2l/jobs/create-job/','type':'Post'})  
            return render_to_response('job/create_job.html',variables)
    else:				
        form=JobForm()    
        variables = RequestContext(request,{'form':form,'action':'/l2l/jobs/create-job/','type':'Post'})    
        return render_to_response('job/create_job.html',variables)

@login_required
def editJob(request,job_id):
	jb = Jobs.objects.get(pk=job_id)	
	if request.method=='POST':
		form=JobForm(request.POST,instance=jb) 
		if form.is_valid():
			jb.title = request.POST["title"]		
			jb.job_position = request.POST["job_position"]		
			jb.description = request.POST["description"]		
			jb.dead_line = request.POST["dead_line"]		
			jb.vacancies = request.POST["vacancies"]		
			jb.company = request.POST["company"]		
			jb.company_website = request.POST["company_website"]		
			jb.location = request.POST["location"]		
			jb.job_status = request.POST["job_status"]		
			jb.practice_areas = request.POST["practice_areas"]	
			jb.save()
			title="Successfully edited"
			message="Job edited."
			variables = RequestContext(request,{'title':title,'message':message}) 
			return render_to_response('pages/success_page.html',variables)
		else:
			err=form.errors
			action = "/l2l/jobs/edit-job/%s"%(job_id)
			variables = RequestContext(request,{'form':form,'message':err,'action':action,'type':'Edit'})  
			return render_to_response('job/create_job.html',variables)
	else:
		form=JobForm(instance=jb) 
		action = "/l2l/jobs/edit-job/%s"%(job_id)
		variables = RequestContext(request,{'form':form,'action':action,'type':'Edit'})    
		return render_to_response('job/create_job.html',variables)

@login_required
def deleteJob(request,job_id):
	jb = Jobs.objects.get(pk=job_id)
	jb.job_status=False
	jb.save()
	title="Successfully deleted"
	message="Job deleted."
	variables = RequestContext(request,{'title':title,'message':message}) 
	return render_to_response('pages/success_page.html',variables)
		
@login_required
def applyForJob(request,job_id):
    if request.method == "POST": 
        cv = request.FILES["applicant_cv"] 
        #job_creater = Jobs.objects.get(pk=job_id)            
        form = ApplyJobForm(request.POST)
        a = form.save()
        job=Jobs.objects.get(pk=job_id)
        a.job=job
        a.applicant = request.user
        a.applicant_cv = cv
        a.save()
        email= job.posted_by.email
        #email= 'sarojsilwal@gmail.com' 
        addlink = '<br/><a href="http://www.destructible.me/l2l/users/notifications">Visit L2L for more info</a>'           
        email_message= "%s %s(%s) has applied for the position %s in L2L.<br/>Email-address : %s<br/>Job Title : %s%s"%(request.user.first_name,request.user.last_name,request.user.username,job.job_position,request.user.email,job.title,addlink)
        msg=EmailMessage('Job applied in L2L',email_message,'no-reply@lawyertolawyerny.com',[email])
        msg.attach(str(cv), cv.read(), cv.content_type)
        msg.content_subtype="html"
        msg.send()
        message="Application sent successfully for the applied job."
        title="Job applied."
        variables = RequestContext(request,{'title':title,'message':message}) 
        return render_to_response('pages/success_page.html',variables)
        
    else:		
        j = Jobs.objects.get(pk=job_id)
        form=ApplyJobForm(instance=j)
        variables = RequestContext(request,{'form':form,'job':j})    
        return render_to_response('job/apply_job.html',variables)

@login_required
def viewJob(request,job_id):
    jobs=Jobs.objects.filter(status=1)
    title="Job Board"
    message=""
    variables = RequestContext(request,{'jobs':'jobs','title':title,'message':message})    
    return render_to_response('job/view_jobs.html',variables)


@login_required
def viewAll(request):	
	if request.GET['type'] == 'attorney':
		jobs=Jobs.objects.filter(job_status=1,posted_by=request.user.id)
	else:
		jobs=Jobs.objects.filter(job_status=1)
	title="Job Board"
	message=""
	variables = RequestContext(request,{'jobs':jobs,'title':title,'message':message})    
	return render_to_response('job/view_jobs.html',variables)
